package com.mark.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.mark.enums.ResponseCodeEnum;

import java.io.Serializable;

/**
 * Created by Choisaaaa on 2018/6/21.
 */

//当序列化Json对象时，如果是null的对象，key也会消失
//@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable {
    private int code;
    private String msg;
    private T data;

    private ServerResponse(int code){
        this.code = code;
    }

    private ServerResponse(int code,T data){
        this.code = code;
        this.data = data;
    }

    private ServerResponse(int code,String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ServerResponse(int code,String msg){
        this.code = code;
        this.msg = msg;
    }

    @JsonIgnore
    public boolean isSuccess(){
        return  this.code == ResponseCodeEnum.SUCCESS.getCode();
    }

    public int getcode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResponseCodeEnum.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return new ServerResponse<T>(ResponseCodeEnum.SUCCESS.getCode(),msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T data){
        return new ServerResponse<T>(ResponseCodeEnum.SUCCESS.getCode(),data);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg,T data){
        return new ServerResponse<T>(ResponseCodeEnum.SUCCESS.getCode(),msg,data);
    }


    public static <T> ServerResponse<T> createByError(){
        return new ServerResponse<T>(ResponseCodeEnum.ERROR.getCode(),ResponseCodeEnum.ERROR.getDesc());
    }


    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
        return new ServerResponse<T>(ResponseCodeEnum.ERROR.getCode(),errorMessage);
    }

    public static <T> ServerResponse<T> createByIllegalArgument(String errorMessage){
        return new ServerResponse<T>(ResponseCodeEnum.ILLEGAL_ARGUMENT.getCode(),errorMessage);
    }

    public static <T> ServerResponse<T> createByException(String errorMessage){
        return new ServerResponse<T>(ResponseCodeEnum.Exception.getCode(),errorMessage);
    }

    //需要登录的构造器
    public static <T> ServerResponse<T> createByNeedLogin(){
        return new ServerResponse<T>(ResponseCodeEnum.NEED_LOGIN.getCode(),ResponseCodeEnum.NEED_LOGIN.getDesc());
    }

    //需要登录的构造器SELLER_NEED_LOGIN
    public static <T> ServerResponse<T> createBySellerNeedLogin(){
        return new ServerResponse<T>(ResponseCodeEnum.SELLER_NEED_LOGIN.getCode(),ResponseCodeEnum.NEED_LOGIN.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
        return new ServerResponse<T>(errorCode,errorMessage);
    }
}
